<% save_url  = defined?(form_url) ? form_url : '#'
   remote_x  = defined?(remote) ? remote : true
   read_only = user_signed_in? && (!dto_allowed? || (defined?(read_only) && read_only))
   new       = resource.new_record?
   prefix    = "#{(resource.is_a?(Cms::Ci) ? resource.ciClassName : (resource.is_a?(Cms::Relation) ? resource.relationName : resource.class.name)).gsub(/\W/, '_')}_#{resource.id}_#{random_dom_id}"
   block     = resource.persisted? && resource.errors.blank? %>
<div>
  <div id="<%= "#{prefix}_div" %>" class="edit_form">
    <% unless read_only %>
      <div class="edit-form-controls"></div>
    <% end %>
    <% html_options = {:id => "#{prefix}_form", :class => "form-horizontal #{'editing' unless read_only || block}"}
       form_options = {:remote => remote_x,
                       :url    => save_url,
                       :html   => html_options,
                       :method => defined?(form_method) ? form_method : (new ? :post : :put)} %>
    <%= form_for resource, form_options do |f| %>
      <%= error_messages_for(resource) %>
      <% content_template = if defined?(content)
                              content
                            elsif resource.is_a?(Cms::Ci)
                              'cms/cis/ci'
                            elsif resource.is_a?(Cms::Relation)
                              'cms/relations/relation'
                            end %>
      <%= resource_key = resource.is_a?(Cms::Ci) ? :ci : (resource.is_a?(Cms::Relation) ? :relation : :resource)
          render content_template,
                 {:f => f,
                 resource_key => resource}.merge(defined?(content_locals) ? content_locals : {}) %>
      <% unless read_only %>
        <div id="<%= prefix %>_form_actions" class="form-actions" <%= "style=display:none" if block %>>
        	<%= link_to(content_tag(:button, icon('trash', 'Delete', 'fa-inverse'), :class => 'btn btn-danger secondary-action'),
                      destroy_url,
                      :confirm => 'Are you sure you want to proceed with delete?',
                      :method  => :delete,
                      :remote  => remote_x) unless new || !defined?(destroy_url) || destroy_url.blank? %>
          <%= link_to_function(icon('times', 'Cancel'), "cancel_#{prefix}()", :class => 'btn cancel') %>
          <%= link_to_function(icon('check', 'Save'),
                               'if (!$j(this).hasClass("disabled")) $j(this).parents("form").trigger("beforeSave").submit();',
                               :class => 'btn btn-success save') %>
        </div>
      <% end %>
    <% end %>
  </div>
  <script type="text/javascript">
    window.edit_<%= prefix %> = function() {
      enable_edit('<%= prefix %>_div');
      $j('#<%= prefix %>_form_edit_link, #<%= prefix %>_form_cancel_link, #<%= prefix %>_form_save_link, #<%= prefix %>_form_actions').toggle();
    };

    window.cancel_<%= prefix %> = function() {
      if ($j("#<%= prefix %>_form").attr("data-dirty") == "true" || <%= resource.errors.present? ? 'true' : 'false' %> || <%= resource.new_record? %>) {
        <% if !defined?(cancel_url) || cancel_url.blank?
             if save_url.is_a?(Hash)
               cancel_url = save_url.merge({:action => new ? :index : :edit})
             else
               cancel_url = URI.parse(save_url)
               cancel_url.path += '/edit' unless new
               cancel_url = cancel_url.to_s
             end
           end %>
        <% if remote_x %>
          <%= remote_function(:url => cancel_url, :method => :get) %>
        <% else %>
          window.location.href = "<%= cancel_url %>";
        <% end %>
      }
      else {
        disable_edit('<%= prefix %>_div');
        toggle(['<%= prefix %>_form_edit_link', '<%= prefix %>_form_cancel_link', '<%= prefix %>_form_save_link', '<%= prefix %>_form_actions']);
      }
    };

    $j("#<%= prefix %>_form")
        .change(function () {
                  $j(this).attr("data-dirty", "true");
                })
        .submit(function (e) {
                  var form = $j(this);
                  if (this.checkValidity && !this.checkValidity()) {
                    var invalidFields = form.find(":invalid"),
                        errorsDiv     = form.find('div.error-messages'),
                        errorList     = errorsDiv.find("ul");
                    errorList.html('');
                    invalidFields.each(function (index, field) {
                      var label = $j(field).parents(".control-group").find("label.control-label").text() || field.id || "",
                          message = field.validationMessage || 'Invalid value.';
                      errorList.append("<li><a onclick=\"getElementById('" + field.id + "').focus()\">" + label + "</a>: " + message + "</li>");
                    });
                    errorsDiv.show()[0].scrollIntoView(false);
                    invalidFields[0].focus();
                    e.preventDefault();
                    return false;
                  }
                  else {
                    form.find(".form-actions .btn:has(i.fa-check)").addClass('disabled');
                    form.parents(".section").find(".section-heading .menu .btn.save").addClass('disabled');
                    form.parents(".section").find(".section-heading .menu .btn.cancel").addClass('disabled');
                    <% if defined?(busy_block) %>
                      show_busy("<%= escape_javascript(busy_block) %>");
                    <% end %>
                    return true;
                  }
                });

    <% unless read_only %>
      <% control_buttons = [link_to_function(icon('times', 'Cancel'), "cancel_#{prefix}()", :id => "#{prefix}_form_cancel_link", :class => "btn btn-mini cancel", :style => block ? 'display:none' : '' ),
                            link_to_function(icon('check', 'Save'), "$j('##{prefix}_form .form-actions .btn.save').click()", :id => "#{prefix}_form_save_link", :class => "btn btn-mini btn-success save", :style => block ? 'display:none' : '')]
        control_buttons << link_to_function(icon('edit',  'Edit'), "edit_#{prefix}()", :id => "#{prefix}_form_edit_link", :class => "btn btn-mini edit #{'hide' unless block}") unless new %>

      var controlsContainer = $j("#<%= prefix %>_div").parents("div.section").find(".section-heading .menu");
      if (!controlsContainer[0]) {
        controlsContainer = $j("#<%= prefix %>_div").find(".edit-form-controls");
      }
      controlsContainer.html("<%= escape_javascript(raw(control_buttons.join('&nbsp;'))) %>");
    <% end %>

    <% if read_only || block %>
      disable_edit('<%= prefix %>_div');
    <% end %>

    $$('#' + '<%= prefix %>_div' + ' input[type=text]').each(function(el) {
      if (el.readOnly) {
        el.observe("click", function () {
          if (this.doSelect) {
            this.select();
            this.doSelect = false;
          }
        });
        el.observe("focus", function () {
          this.doSelect = true;
        });
      }
    });

  $j('.control-label a').tooltip();

  </script>
</div>
